﻿using Loli.Library.WinIo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PeopleSimulator
{
    public partial class CreateScriptForm : Form
    {
        private KeyCodes _KeyCode = KeyCodes.None;
        private int _Deley = 0;
        private int _Interval = 0;
        BindingList<Detail> _Details = null;

        public Script Script
        {
            get;
            set;
        }

        public CreateScriptForm()
        {
            InitializeComponent();
            this._Details = new BindingList<Detail>();

            this.lbList.DataSource = this._Details;
            this.lbList.DisplayMember = "DisplayString";
        }



        private void Init()
        {
            this._Deley = 300;
            this._Interval = 200;
            this.tbDeley.Text = this._Deley.ToString();
            this.tbInterval.Text = this._Interval.ToString();
            this.tbKeyCode.Text = string.Empty;
            if (this._Details.Count >= 0)
            {
                this._Details.Clear();
            }

            if (this.Script == null)
            {
                this.tbName.Text = "新脚本 - " + DateTime.Now.ToString("mmss");
                this.Text = "创建脚本";
            }
            else
            {
                this.tbName.Text = this.Script.Name;
                foreach (var detail in this.Script.Details)
                {
                    this._Details.Add(detail);
                }
                this.Text = "修改脚本";
            }



        }


        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            this.Init();
        }


        private void butInsert_Click(object sender, EventArgs e)
        {
            Detail detail = this.CheckData();

            if (detail != null)
            {
                if (this.lbList.SelectedIndex >= 0)
                {
                    this._Details.Insert(this.lbList.SelectedIndex, detail);
                }
                else
                {
                    this._Details.Add(detail);
                }
                this.tbKeyCode.Focus();
            }
        }

        private void butAdd_Click(object sender, EventArgs e)
        {
            Detail detail = this.CheckData();

            if (detail != null)
            {
                this._Details.Add(detail);
                this.tbKeyCode.Focus();
            }
        }

        private Detail CheckData()
        {
            if (_KeyCode == KeyCodes.None)
            {
                MessageBox.Show(this, "按键输入错误，请重新输入！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbKeyCode.Focus();
                return null;
            }


            bool isSuccess = int.TryParse(tbDeley.Text, out this._Deley);
            if (!isSuccess)
            {
                MessageBox.Show(this, "延时输入错误，请重新输入！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbDeley.Focus();
                return null;
            }

            isSuccess = int.TryParse(tbInterval.Text, out this._Interval);
            if (!isSuccess)
            {
                MessageBox.Show(this, "持续时间输入错误，请重新输入！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbInterval.Focus();
                return null;
            }

            Detail detail = new Detail() { KeyCodes = this._KeyCode, Deley = this._Deley, Interval = this._Interval };

            return detail;
        }

        private void tbKeyCode_KeyDown(object sender, KeyEventArgs e)
        {
            _KeyCode = (KeyCodes)e.KeyCode;
            tbKeyCode.Text = _KeyCode.ToString();
            tbKeyCode.SelectionStart = tbKeyCode.Text.Length;
        }

        private void tbKeyCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void butRemove_Click(object sender, EventArgs e)
        {
            int index = this.lbList.SelectedIndex;

            if (index >= 0)
            {
                Detail detail = this.lbList.SelectedItem as Detail;
                this._Details.Remove(detail);

                if (this.lbList.Items.Count > index)
                {
                    this.lbList.SelectedIndex = index;
                }
                else if (this.lbList.Items.Count <= index)
                {
                    this.lbList.SelectedIndex = this.lbList.Items.Count - 1;
                }
            }
        }

        private void butUp_Click(object sender, EventArgs e)
        {
            int index = this.lbList.SelectedIndex;
            if (index >= 0)
            {
                Detail detail = this.lbList.SelectedItem as Detail;

                if (index - 1 >= 0)
                {
                    Detail up = this.lbList.Items[index - 1] as Detail;

                    this._Details[index] = up;
                    this._Details[index - 1] = detail;

                    this.lbList.SelectedIndex = index - 1;
                }
            }
        }

        private void butDown_Click(object sender, EventArgs e)
        {
            int index = this.lbList.SelectedIndex;
            if (index >= 0)
            {
                Detail detail = this.lbList.SelectedItem as Detail;

                if (index + 1 < this.lbList.Items.Count)
                {
                    Detail down = this.lbList.Items[index + 1] as Detail;
                    this._Details[index] = down;
                    this._Details[index + 1] = detail;

                    this.lbList.SelectedIndex = index + 1;
                }
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            this.Hide();
        }

        private void butClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void butSave_Click(object sender, EventArgs e)
        {
            if (this._Details.Count <= 0)
            {
                MessageBox.Show(this, "还没有按键数据，请先新增！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                butAdd.Focus();
                return;
            }

            string name = tbName.Text.Trim();
            if (string.IsNullOrEmpty(name))
            {
                MessageBox.Show(this, "脚本名称不能为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                tbName.Focus();
                return;
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Script = new Script()
            {
                CreateTime = DateTime.Now,
                Details = this._Details.ToList(),
                Name = name
            };
            this.Close();

        }

        private void butRecord_Click(object sender, EventArgs e)
        {
            KeyRecordForm KeyRecord = new KeyRecordForm();
            DialogResult result = KeyRecord.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                List<Detail> list = KeyRecord.Details;
                //计算延时
                DateTime now = DateTime.Now;
                for (int i = 0; i < list.Count; i++)
                {
                    Detail detail = list[i];
                    if (i != 0)
                    {
                        TimeSpan span = detail.CreateTime - now;
                        this._Details.Add(new Detail() { Deley = Convert.ToInt32( span.TotalMilliseconds) });
                    }

                    now = detail.CreateTime;
                    this._Details.Add(detail);
                }
            }
        }
    }
}
